﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Data.SqlClient;
using System.Text;
using IDAL;

namespace DAL
{
    public class AboutAndCareerContentDAL : IAboutAndCareerContent
    {
        public DataBase DB = new DataBase();

        public string GetAboutContent()
        {
            string sql = "SELECT * FROM AboutCareerContent WHERE ID = 1";
            string content = "";
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(sql);
            while (reader.Read())
            {
                content = reader["Content"] is DBNull ? "" : reader["Content"].ToString();
            }
            DB.CloseDB();
            return content;
        }
        public string GetCareerContent()
        {
            string sql = "SELECT * FROM AboutCareerContent WHERE ID = 2";
            string content = "";
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(sql);
            while (reader.Read())
            {
                content = reader["Content"] is DBNull ? "" : reader["Content"].ToString();
            }
            DB.CloseDB();
            return content;
        }
        private int GetCount(int ID)
        {
            string sql = "SELECT COUNT(*) AS c FROM AboutCareerContent WHERE ID=" + ID;
            int count = 0;
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(sql);
            while (reader.Read())
            {
                try
                {
                    count = reader["c"] is DBNull ? 0 : Int32.Parse(reader["c"].ToString());
                }
                catch {
                    count = 0;
                }
            }
            DB.CloseDB();
            return count;
        }
        public int UpdateAboutContent(string content)
        {
            return UpdateContent(1, content);
        }

        public int UpdateCareerContent(string content)
        {
            return UpdateContent(2, content);
        }

        private int UpdateContent(int id, string content)
        {
            string sqlUpdate = " UPDATE AboutCareerContent SET Content = '" + content + "' WHERE ID = " + id;
            string sqlInsert = " INSERT INTO AboutCareerContent([ID],[Content]) VALUES(" + id + ",'" + content + "')";
            string sqlDelete = "DELETE AboutCareerContent WHERE ID=" + id + ";";
            string sql = "";
            int count = GetCount(id);
            if (count > 1)
            {
                sql = sqlDelete + sqlInsert;
            }
            else if (count == 1)
            {
                sql = sqlUpdate;
            }else
            {
                sql = sqlInsert;
            }
            int isSuccess = -1;
            isSuccess = DB.UpdateRecords(sql);
            return isSuccess;
        }
    }
}
